package com.song.ejf.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.song.ejf.dao.BaseDAO;
import com.song.ejf.page.Page;
import com.song.ejf.pojo.SResource;
import com.song.ejf.service.ResourceService;

@Service("resouceService")
public class ResouceServiceImpl implements ResourceService {
	@Autowired
	BaseDAO baseDAO;

	@Override
	public void loadResourcePage(Page page, String resName, String resUrl) throws Exception {
		Map<String, String> paraMap = new HashMap<String, String>();
		String selectSql = "select r_id,resource_name,resource_url,last_modify_time,status ";
		String mainSql = " select e.r_id,e.resource_name,e.resource_url,e.last_modify_time," +
				"e.status,rownum RN from s_resource e where 1=1 ";
		if(StringUtils.isNotBlank(resName)){
			paraMap.put("resName", "%" + resName.trim() + "%");
			mainSql += " and e.resource_name like :resName ";
		}
		if(StringUtils.isNotBlank(resUrl)){
			paraMap.put("resUrl", "%" + resName.trim() + "%");
			mainSql += " and e.resource_url like :resUrl ";
		}
		String order = " order by e.resource_name";
//		String pageSql =  " where RN > " + page.getStartIndex() + " and RN < " + page.getEndIndex() + " ";
		baseDAO.sqlPageQuery(page, selectSql, mainSql, order, paraMap);
	}

	@Override
	public boolean delResource(String[] checkItems)  throws Exception{
		if(checkItems.length <= 0){
			return false;
		}
		for (int i = 0; i < checkItems.length; i++) {
			baseDAO.delById(SResource.class, checkItems[i]);
		}
		return true;
	}

	@Override
	public void saveOrUpdateResource(SResource resource)  throws Exception{
		baseDAO.saveOrUpdate(resource);
		
	}

	@Override
	public SResource findResourceById(String id) throws Exception {
		return (SResource)baseDAO.loadById(SResource.class, id);
	}

	@Override
	public List<Map<String, String>> loadAllRes() throws Exception {
		String mainSql = "select sr.r_id,sr.resource_name,sr.resource_url from s_resource sr where 1=1 order by sr.resource_name";
		return baseDAO.sqlQuery(mainSql, null);
	}
}
